home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Tutorial / Cookbook / 41.PieChart / PieChartView.m < prev    next >
Text File  |  1995-06-12  |  24KB  |  268 lines

  1. #import "PieChartView.h"
  2. #import <strings.h>
  3. #import <appkit/Font.h>
  4. #import "slice.h"
  5. #import <appkit/Control.h>
  6. #import <appkit/Application.h>
  7. #import <appkit/Pasteboard.h>
  8. #import <appkit/Matrix.h>
  9. #import <dpsclient/wraps.h>
  10.  
  11. @implementation PieChartView:View
  12.  
  13. +newFrame:(const NXRect *)tF
  14. {
  15. int i;
  16.    // we are using the Appkit "newFrame" plus our own additions
  17.    self = [super newFrame:tF];  
  18.  
  19.    fontID = [Font newFont:"Helvetica"
  20.                   size:14.0
  21.           style:0
  22.           matrix:NX_IDENTITYMATRIX];
  23.     numWedges = 3;
  24.     wedgeValue[0] = 100.0; strcpy(labels[0], "Apples"); grays[0] = 1.0;
  25.     wedgeValue[1] = 200.0; strcpy(labels[1], "Bananas"); grays[1] = 0.333;
  26.     wedgeValue[2] = 300.0; strcpy(labels[2], "Oranges"); grays[2] = 0.666;
  27.     pieSize = 100.0;
  28.     pieFontSize = 14.0;
  29.     for (i=3; i<10; i++) {
  30.            grays[i] = ((float) i)/10.0;
  31.     }
  32.     [self normalize];
  33.     return self;
  34. }
  35.  
  36. // --------------- methods that respond to user actions start here  --------------
  37.  
  38. - takeValueFromMatrix:sender
  39. {
  40.     int i;
  41.     i = [sender selectedRow];
  42.     if (i >= numWedges) {
  43.         numWedges = i+1;
  44.         [numWedgesText setIntValue:numWedges];
  45.     }
  46.     wedgeValue[i] = [sender floatValue];
  47.     [self normalize];
  48.     [self display];
  49.     return self;
  50. }
  51.  
  52. - takeLabelFromMatrix:sender
  53. {
  54.     int i;
  55.     i = [sender selectedRow];
  56.     if (i >= numWedges) {
  57.         numWedges = i+1;
  58.         [numWedgesText setIntValue:numWedges];
  59.     }
  60.     strcpy(labels[i],[sender stringValue]);
  61.     [self display];
  62.     return self;
  63. }
  64.  
  65. - takeGraysFromMatrix:sender
  66. {
  67.     int i;
  68.     i = [sender selectedRow];
  69.     grays[i] = [sender floatValue];
  70.     [self display];
  71.     return self;
  72. }
  73.  
  74. - takePieSizeFrom:sender
  75. {
  76. float tmpFlt;
  77.     tmpFlt = [sender floatValue];
  78.     [pieSizeText setIntValue:tmpFlt];
  79.     [self setPieSize:tmpFlt];
  80.     return self;
  81. }
  82.  
  83. -takeFontSizeFrom:sender
  84. {
  85. float tmpFlt;
  86.     tmpFlt = [sender floatValue];
  87.     [labelLineLengthText setIntValue:tmpFlt];
  88.     [self setPieFontSize:tmpFlt];
  89.     return self;
  90. }
  91.  
  92. -takeNumWedgesFrom:sender
  93. {
  94.     numWedges = [sender intValue];
  95.     [numWedgesText setIntValue:numWedges];
  96.     [self normalize];
  97.     return self;
  98. }
  99.  
  100. - copy:sender
  101.    id pb = [NXApp pasteboard];  /* global Pasteboard object */ 
  102.    NXStream  *st;               /* stream to collect data in */ 
  103.    char      *data;             /* actual data buffer */ 
  104.    int       length;            /* length of data */ 
  105.    int       maxLength;         /* (not used here) */
  106.  
  107.    // To see how to use the pasteboard see page 10-33 of
  108.    // the SysRefMan
  109.    // declare that we will supply a single type of data: PostScript
  110.    [pb declareTypes:&NXPostScriptPboard num:1 owner:self];
  111.  
  112.    /* get a stream which writes to memory */ 
  113.    st = NXOpenMemory( NULL, 0, NX_WRITEONLY );
  114.  
  115.    /* write PostScript code for this view into the stream */ 
  116.    [self copyPSCodeInside:NULL to:st];
  117.  
  118.    /* get actual data buffer from stream */ 
  119.    NXGetMemoryBuffer( st, &data, &length, &maxLength );
  120.  
  121.    /* write PostScript data to pasteboard */ 
  122.    [pb writeType:NXPostScriptPboard data:data length:length];
  123.  
  124.    /* deallocate stream, including its buffer */ 
  125.    NXCloseMemory( st, NX_FREEBUFFER );
  126.  
  127.    return self; 
  128. }
  129.  
  130.  
  131. /* make this view accept first responder so it will understand copy */
  132. -(BOOL)acceptsFirstResponder
  133. {
  134.     return (YES);
  135. }
  136.  
  137. -resignFirstResponder
  138. {
  139.     return self;
  140. }
  141.  
  142.  
  143.  
  144. // --------------- end of action methods  --------------
  145. // --------------- start of outlets  --------------
  146. - setNumWedgesText:anObject
  147. {
  148.     numWedgesText = anObject;
  149.     return self;
  150. }
  151.  
  152. - setPieSizeText:anObject
  153. {
  154.     pieSizeText = anObject;
  155.     return self;
  156. }
  157.  
  158. - setLabelLineLengthText:anObject
  159. {
  160.     labelLineLengthText = anObject;
  161.     return self;
  162. }
  163.  
  164. - setPieFontText:anObject
  165. {
  166.     pieFontText = anObject;
  167.     return self;
  168. }
  169.  
  170. // --------------- end of outlets  --------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  171.  
  172.  
  173. -setPieSize: (float) size
  174. {
  175.     pieSize = size;
  176.     [self display];
  177.     return self;
  178. }
  179.  
  180. -setPieFontSize: (float) size
  181. {
  182.     pieFontSize = size;
  183.     [self display];
  184.     return self;
  185. }
  186.  
  187. -setPieFont: (char *) fontName
  188. {
  189.     strcpy(pieFont, fontName);
  190.     [self display];
  191.     return self;
  192. }
  193.  
  194. -normalize;  /* make sure that all of the values add up to 360 */
  195. {
  196. int i;
  197. float total=0.0;
  198.     for (i=0; i<numWedges; i++) {
  199.         total += wedgeValue[i];
  200.     }
  201.     for(i=0; i<numWedges; i++) {
  202.         normData[i] = wedgeValue[i]*360.0/total;
  203.     }
  204.     [self display];  // we always want to do this, avoid duplicaion
  205.     return self;
  206. }
  207.  
  208.  
  209. -addPieWedge: (float)value: (char *)inLabel: (float)inGray;
  210. {
  211.     wedgeValue[numWedges] = value;
  212.     strcpy(labels[numWedges], inLabel);
  213.     grays[numWedges] = inGray;
  214.     numWedges += 1;
  215.     [numWedgesText setIntValue:numWedges];
  216.     [self normalize];
  217.     return self;
  218. }
  219.  
  220.  
  221. -removePieWedge: (int)index;
  222. {
  223. int i;
  224.     for (i=index; i<=numWedges-1; i++) {
  225.         wedgeValue[i-1] = wedgeValue[i];
  226.         strcpy(labels[i-1],labels[i]);
  227.         grays[i-1] = grays[i];
  228.     }
  229.     wedgeValue[i] = 0.0; // not really necessary
  230.     numWedges -= 1;
  231.     [numWedgesText setIntValue:numWedges];
  232.     [self normalize];
  233.     return self;
  234. }
  235.  
  236.  
  237. -setPieWedge: (int)wedgeNumber: (float)value: (char *)inLabel: (float)inGray;
  238. {
  239.     wedgeValue[wedgeNumber] = value;
  240.     strcpy(labels[wedgeNumber], inLabel);
  241.     grays[wedgeNumber] = inGray;
  242.     [self display];
  243.     return self;
  244. }
  245.  
  246.  
  247. - drawSelf:(NXRect*)r :(int)c
  248. {
  249.     int i, c_x, c_y;
  250.     float total = 0.0;
  251.     c_x = bounds.size.width/2.0 + bounds.origin.x;
  252.     c_y = bounds.size.height/2.0 + bounds.origin.y;
  253.     [self translate:c_x :c_y];
  254.     [fontID set];
  255.     PSsetgray(1.0); // for a white background
  256.     NXRectFill(&bounds);
  257.     PSsetgray(0.0);
  258.     for (i=0; i<numWedges; i++)
  259.     {
  260.      drawSlice(grays[i], pieSize, total, total+normData[i], pieFontSize, labels[i]);
  261.        total = total + normData[i];
  262.     }
  263.     return self;
  264. }
  265.  
  266.  
  267.